package com.xyrl.project.manager.system.controller;

import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.xyrl.common.base.exception.XyrlException;
import com.xyrl.common.core.controller.BaseController;
import com.xyrl.common.util.domain.QueryRequest;
import com.xyrl.project.common.annotation.Log;
import com.xyrl.project.common.system.entity.User;
import com.xyrl.project.common.system.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.Map;

/**
 * 客户经理管理
 *
 * @author zhongp
 * @version 1.0
 * @className SalesManController
 * @company 星纭融链科技服务有限公司
 * @date 2019-12-26 17:18:00
 * @Description
 */
@Slf4j
@Validated
@RestController
@RequestMapping("salesMan")
public class SalesManController extends BaseController {
    String message = "";
    @Autowired
    private UserService userService;

    @GetMapping
    @RequiresPermissions("salesMan:view")
    public Map<String, Object> userList(QueryRequest queryRequest, User user) {
        user.setUserType(User.USER_TYPE_SALESMAN);
        return getDataTable(userService.findUserDetail(user, queryRequest));
    }

    @Log("新增客户经理")
    @PostMapping
    @RequiresPermissions("salesMan:add")
    public void addUser(@Valid User user) throws XyrlException {
        try {
            user.setUserType(User.USER_TYPE_SALESMAN);
            this.userService.createUser(user);
        }catch (XyrlException e) {
            message = "删除用户失败";
            log.error(message, e);
            throw new XyrlException(message);
        }  catch (Exception e) {
            log.error("添加客户经理失败", e);
            throw new XyrlException("添加客户经理失败");
        }
    }


    @Log("修改客户经理")
    @PutMapping
    @RequiresPermissions("salesMan:update")
    public void updateUser(@Valid User user) throws XyrlException {
        try {
            this.userService.updateUser(user);
        } catch (XyrlException e) {
            message = "删除用户失败";
            log.error(message, e);
            throw new XyrlException(message);
        } catch (Exception e) {
            message = "修改用户失败";
            log.error(message, e);
            throw new XyrlException(message);
        }
    }

    @Log("删除客户经理")
    @DeleteMapping("/{userIds}")
    @RequiresPermissions("salesMan:delete")
    public void deleteUsers(@NotBlank(message = "{required}") @PathVariable String userIds) throws XyrlException {
        try {
            String[] ids = userIds.split(StringPool.COMMA);
            this.userService.deleteUsers(ids);
        } catch (XyrlException e) {
            message = "删除用户失败";
            log.error(message, e);
            throw new XyrlException(message);
        } catch (Exception e) {
            message = "删除用户失败";
            log.error(message, e);
            throw new XyrlException(message);
        }
    }

    @PutMapping("password/reset")
    @Log("密码重置")
    @RequiresPermissions("salesMan:reset")
    public void resetPassword(@NotBlank(message = "{required}") String usernames) throws XyrlException {
        try {
            String[] usernameArr = usernames.split(StringPool.COMMA);
            this.userService.resetPassword(usernameArr);
        } catch (XyrlException e) {
            message = "删除用户失败";
            log.error(message, e);
            throw new XyrlException(message);
        } catch (Exception e) {
            message = "重置用户密码失败";
            log.error(message, e);
            throw new XyrlException(message);
        }
    }

}
